package com.baidu.web;

import com.baidu.common.bean.Goods;
import com.baidu.common.bean.GoodsType;
import com.baidu.common.bean.User;
import com.baidu.common.exception.WebException;
import com.baidu.common.finalword.WebConstant;
import com.baidu.service.GoodsService;
import com.baidu.service.TypeService;
import com.baidu.service.impl.GoodsServiceImpl;
import com.baidu.service.impl.TypeServiceImpl;
import com.mysql.jdbc.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/8/20/9:21
 */
public class GoodsContorller extends HttpServlet {
    private GoodsService goodsService = new GoodsServiceImpl();
    private TypeService typeService = new TypeServiceImpl();

    private List<Goods> shoppingCart = new ArrayList<>();

    public GoodsContorller() throws WebException {
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String requestPath = req.getRequestURL().toString();

        String idStr = req.getParameter("id");
        Integer id = StringUtils.isNullOrEmpty(idStr)?0:Integer.parseInt(idStr);

        try {
            Goods goods = goodsService.get(id);
            if (requestPath.contains(WebConstant.GOODS_LISTSTR)){
                List<Goods> goodsList = goodsService.queryGoodsTypeList();
                req.setAttribute("goodsList",goodsList);
                req.getRequestDispatcher("/WEB-INF/page/goodsList.jsp").forward(req,resp);
            }else if (requestPath.contains(WebConstant.GOODS_ADD_CAR_STR)){
                shoppingCart.add(goods);
                req.setAttribute(WebConstant.GOODS_MESSAGE,"商品添加购物车成功！");
                req.getRequestDispatcher(WebConstant.GOODS_INFO_PAGE_PATH).forward(req,resp);
            }else if (requestPath.contains(WebConstant.GOODS_TO_UPDATE_STR)){
                List<GoodsType> types = typeService.queryGoodsTypeList();
                req.setAttribute("types",types);
                req.setAttribute("goods",goods);
                req.getRequestDispatcher("/WEB-INF/page/updateGoods.jsp").forward(req,resp);
            }else if (requestPath.contains(WebConstant.GOODS_TO_ADD_STR)){
                req.getRequestDispatcher("/WEB-INF/page/addGoods.jsp").forward(req,resp);
            }else if (requestPath.contains(WebConstant.GOODS_DELETESTR)){
                int success = goodsService.delete(id);
                if (success==1){
                    req.setAttribute(WebConstant.GOODS_MESSAGE,"商品删除成功！");
                    req.getRequestDispatcher(WebConstant.GOODS_INFO_PAGE_PATH).forward(req,resp);
                    return;
                }

                req.setAttribute(WebConstant.GOODS_MESSAGE,"商品删除失败！");
                req.getRequestDispatcher(WebConstant.GOODS_INFO_PAGE_PATH).forward(req,resp);
            }else if (requestPath.contains(WebConstant.GOODS_UPDATE_STR)){
                Goods updateGoods = initGoods(id, req);

                int i = goodsService.update(updateGoods);
                if (i==1){
                    req.setAttribute(WebConstant.GOODS_MESSAGE,"商品修改成功！");
                    req.getRequestDispatcher(WebConstant.GOODS_INFO_PAGE_PATH).forward(req,resp);
                    return;
                }
                req.setAttribute(WebConstant.GOODS_MESSAGE,"商品修改失败！");
                req.getRequestDispatcher(WebConstant.GOODS_INFO_PAGE_PATH).forward(req,resp);
            }else if (requestPath.contains(WebConstant.GOODS_ADDSTR)){
                deleteGoodsById(req, resp, id);
            }else if (requestPath.contains(WebConstant.GOODS_TO_CAR_STR)){
                long totalMoney = getTotalMoney(shoppingCart);
                req.setAttribute("totalMoney",totalMoney);
                req.setAttribute("shoppingCar",shoppingCart);
                req.getRequestDispatcher("/WEB-INF/page/shoppingCar.jsp").forward(req,resp);
            }else if (requestPath.contains(WebConstant.GOODS_REMOVE_CAR_GOODS_STR)){
                shoppingCart.removeIf(next -> next.getId().equals(id));
                long totalMoney = getTotalMoney(shoppingCart);
                req.setAttribute("totalMoney",totalMoney);
                req.setAttribute("shoppingCar",shoppingCart);
                req.getRequestDispatcher("/WEB-INF/page/shoppingCar.jsp").forward(req,resp);
            }else if (requestPath.contains(WebConstant.GOODS_TO_ACCOUNT_STR)){
                User user = (User)req.getSession().getAttribute("user");
                if (user==null){
                    req.setAttribute(WebConstant.GOODS_MESSAGE,"请先登录！！！");
                    req.setAttribute("toLogin",true);
                    req.getRequestDispatcher(WebConstant.GOODS_INFO_PAGE_PATH).forward(req,resp);
                }
            }else{
                req.getRequestDispatcher(WebConstant.GOODS_INFO_PAGE_PATH).forward(req,resp);
            }
        } catch (WebException e) {
            e.printStackTrace();
            req.setAttribute(WebConstant.GOODS_MESSAGE,e.getMessage());
            req.getRequestDispatcher(WebConstant.GOODS_INFO_PAGE_PATH).forward(req,resp);
        }
    }

    private void deleteGoodsById(HttpServletRequest req, HttpServletResponse resp, Integer id) throws WebException {
        try {
            Goods addGoods = initGoods(id, req);
            int i = goodsService.add(addGoods);
            if (i==1){
                req.setAttribute(WebConstant.GOODS_MESSAGE,"商品添加成功！");
                req.getRequestDispatcher(WebConstant.GOODS_INFO_PAGE_PATH).forward(req,resp);
                return;
            }
            req.setAttribute(WebConstant.GOODS_MESSAGE,"商品添加失败！");
            req.getRequestDispatcher(WebConstant.GOODS_INFO_PAGE_PATH).forward(req,resp);
        } catch (ServletException | IOException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  {

    }

    public Goods initGoods(Integer id,HttpServletRequest req){
        Goods goods = new Goods();

        goods.setId(id);
        goods.setName(req.getParameter("name"));
        goods.setDescribe(req.getParameter("describe"));
        goods.setTitle(req.getParameter("title"));
        GoodsType type = new GoodsType();
        String typeStr = req.getParameter("type");
        type.setName(typeStr);
        goods.setGoodsType(type);
        goods.setQuantity(Integer.parseInt(req.getParameter("quantity")));
        goods.setPrice(new BigDecimal(req.getParameter("price")));

        return goods;
    }

    public long getTotalMoney(List<Goods> list){
        long totalMoney = 0L;
        for (Goods goods : list) {
            totalMoney+=goods.getPrice().longValue();
        }
        return totalMoney;
    }
}
